#include "pagebrake.h"
#include "ui_pagebrake.h"

PageBrake::PageBrake(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::PageBrake)
{
    ui->setupUi(this);
    setAutoFillBackground(true);  //设置自动填充
    QPalette palette = BKGCLR;
    setPalette(palette);

    m_lBrkStatNameList <<"主风压力(bar)"
                       <<"空气悬挂压力(bar)"
                       <<"制动缸压力(bar)"
                       <<"车辆载重(ton)"
                       <<"主BCE状态"
                       <<"停放制动压力(bar)"
                       <<"保持制动施加状态"
                       <<"满载率(%)";

    m_lBrkStatCntOfOneCar <<1
                          <<2
                          <<2
                          <<1
                          <<1
                          <<1
                          <<1
                          <<1;
    InitBrkStatLabels();
    setDefaultBrkStat();
}

PageBrake::~PageBrake()
{
    delete ui;
}

void PageBrake::InitBrkStatLabels()
{
    m_nStatLabelWidth_Car = MiniCarSaloonWidth+MiniCarHSpace_1220;
    m_nStatLabelSpaceX = MiniCarLeftSpace_1220 - MiniCarHSpace_1220/2;
    m_nStatLabelHeight = 55;
    m_nStatLabelStartY = 15;
    m_nStatLabelSpaceY = 0;

    uint8_t statCnt = m_lBrkStatNameList.length();
    for(int i=0;i<statCnt;i++)
    {
        QLabel *statName = new QLabel(this);
        statName->setStyleSheet(StatLabelStyle);
        statName->setAlignment(Qt::AlignCenter);
        statName->setText(m_lBrkStatNameList.at(i));
        statName->setGeometry(m_nStatLabelSpaceX-190,
                              m_nStatLabelStartY+i*(m_nStatLabelHeight+m_nStatLabelSpaceY),
                              190,m_nStatLabelHeight);

        uint8_t statOneCarCnt = m_lBrkStatCntOfOneCar.at(i);
        QList <QLabel *> _oneStatLabels;_oneStatLabels.clear();
        for(int j=0;j<SaloonCnt;j++)
        {
            for(int k=0;k<statOneCarCnt;k++)
            {
                QLabel *stat = new QLabel(this);
                stat->setStyleSheet(StatLabelStyle);
                stat->setAlignment(Qt::AlignCenter);
                SetPosition(stat,CAR(j),ROW(i),COL(k),TOTAL(statOneCarCnt),j>1);
                _oneStatLabels.append(stat);
            }
        }
        m_lBrkStatList.append(_oneStatLabels);
    }
    CombineTwoLabel(0,0,1);
    CombineTwoLabel(0,0,1);
    CombineTwoLabel(0,1,2);
    CombineTwoLabel(0,1,2);

    CombineTwoLabel(6,0,1);
    CombineTwoLabel(6,0,1);
    CombineTwoLabel(6,1,2);
    CombineTwoLabel(6,1,2);
}

void PageBrake::setDefaultBrkStat()
{
    for(int i=0;i<2;i++)
    {
        setBrkStat(BrkStat_MainRes,i,0);
        setBrkStat(BrkStat_HoldBrk,i,0);
    }

    for(int i=0;i<12;i++)
    {
        setBrkStat(BrkStat_AirSuspPress,i,0);
        setBrkStat(BrkStat_BCPB,i,0);
    }
    for(int i=0;i<6;i++)
    {
        setBrkStat(BrkStat_LocWeight,i,0);
        setBrkStat(BrkStat_PBPress,i,0);
        setBrkStat(BrkStat_LoadFactor,i,0);
    }
    setBrkStat(BrkStat_Master,0,0);
    setBrkStat(BrkStat_Master,2,0);
    setBrkStat(BrkStat_Master,3,0);
    setBrkStat(BrkStat_Master,5,0);
}

//row 和 col 都从0开始
void PageBrake::SetPosition(QWidget* w,int car, uint8_t row, uint8_t col, uint8_t total, bool xReverse)
{
    float OnestatLabelWidth = m_nStatLabelWidth_Car/total;
    if(!xReverse)
        w->setGeometry(m_nStatLabelSpaceX+car*m_nStatLabelWidth_Car+col*OnestatLabelWidth,
                       m_nStatLabelStartY+row*(m_nStatLabelHeight+m_nStatLabelSpaceY),
                       OnestatLabelWidth,m_nStatLabelHeight);
    else
        w->setGeometry(m_nStatLabelSpaceX+car*m_nStatLabelWidth_Car+(total-1-col)*OnestatLabelWidth,
                       m_nStatLabelStartY+row*(m_nStatLabelHeight+m_nStatLabelSpaceY),
                       OnestatLabelWidth,m_nStatLabelHeight);
};

void PageBrake::setBrkStat(uint8_t item,uint8_t index,uint16_t value)
{
    if(index >= m_lBrkStatList[item].length())
        return;
    if(item == BrkStat_MainRes || item == BrkStat_AirSuspPress || item == BrkStat_BCPB || item== BrkStat_PBPress)
    {
        if(value == 0xFF)
            m_lBrkStatList[item][index]->setText(QString("?"));
        else
            m_lBrkStatList[item][index]->setText(QString("%1").arg(value*0.1));
    }
    else if(item == BrkStat_LocWeight)
    {
        if(value == 0xFFFF)
            m_lBrkStatList[item][index]->setText(QString("?"));
        else
            m_lBrkStatList[item][index]->setText(QString("%1").arg(value*0.01));
    }
    else if(item == BrkStat_Master)
    {
        if(value == 0)
            m_lBrkStatList[item][index]->setText(QString("?"));
        else if(value == 1)
            m_lBrkStatList[item][index]->setText(QString("主BCE"));
        else if(value == 2)
            m_lBrkStatList[item][index]->setText(QString("从BCE"));
    }
    else if(item == BrkStat_HoldBrk)
    {
        if(value == 0)
            m_lBrkStatList[item][index]->setText(QString("?"));
        else if(value == 1)
            m_lBrkStatList[item][index]->setText(QString("施加"));
        else if(value == 2)
            m_lBrkStatList[item][index]->setText(QString("缓解"));
    }
    else if(item == BrkStat_LoadFactor)
    {
        if(value == 0xFF)
            m_lBrkStatList[item][index]->setText(QString("?"));
        else
            m_lBrkStatList[item][index]->setText(QString("%1").arg(value));
    }
}

void PageBrake::CombineTwoLabel(int row,int index1,int index2)
{
    float height = m_lBrkStatList[row][index1]->height();
    float width1 = m_lBrkStatList[row][index1]->width();
    float width2 = m_lBrkStatList[row][index2]->width();
    QLabel *pd = m_lBrkStatList[row][index2];
    delete  pd;
    m_lBrkStatList[row].removeAt(index2);
    m_lBrkStatList[row][index1]->resize(width1+width2,height);
}
